package com.ljl.ningwei.controller;

import com.ljl.ningwei.domain.CategorylList2VO;
import com.ljl.ningwei.domain.CategorylListVO;
import com.ljl.ningwei.entity.Category;
import com.ljl.ningwei.common.result.Result;
import com.ljl.ningwei.service.CategoryService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author 吕佳乐
 * 2025-03-13 20:39
 */
@RestController
@RequiredArgsConstructor
@Slf4j
public class CategoryController {

    private final CategoryService categoryService;


    /**
     * 首先展现的是二级标题
     *
     * @return
     */
    @RequestMapping("/category/list")
    public Result<List<CategorylListVO>> list() {
        try {
            // 1. 一次性查询所有分类（包含一级和二级）
            List<Category> categoryList = categoryService.list();
            if (CollectionUtils.isEmpty(categoryList)) {
                return Result.success(Collections.emptyList());
            }
            // 2. 分离一级分类和二级分类
            Map<Long, List<Category>> childrenMap = categoryList.stream()
                    .filter(c -> c.getParentId() != 0) // 二级分类
                    .collect(Collectors.groupingBy(Category::getParentId)); // 按父ID分组

            List<CategorylListVO> results = categoryList.stream()
                    .filter(category -> category.getParentId() == 0)
                    .map(parent -> {
                                CategorylListVO parentVO = new CategorylListVO()
                                        .setCategoryId(parent.getId())
                                        .setCategoryName(parent.getName());
                                List<Category> childrenList = childrenMap.get(parent.getId());
                                if (!CollectionUtils.isEmpty(childrenList)) {
                                    List<CategorylListVO> childrenListVO = childrenList.stream()
                                            .map(children -> {
                                                CategorylListVO childrenVO = new CategorylListVO()
                                                        .setCategoryId(children.getId())
                                                        .setCategoryName(children.getName());
                                                return childrenVO;
                                            }).collect(Collectors.toList());
                                    parentVO.setChildrenList(childrenListVO);
                                }
                                return parentVO;
                            }
                    ).collect(Collectors.toList());
            return Result.success(results);
        } catch (Exception e) {
            // 或者使用日志框架记录详细信息
            log.info("异常: " + e.getMessage());
            throw new RuntimeException(e.getMessage());
        }
    }

    /**
     * 查询二级标题及其以上的子标题
     * @return
     */
    @RequestMapping("/category/listbypid")
    public Result<List<CategorylList2VO>> list(@RequestParam(name = "parentId") Integer parentId) {
        List<Category> categoryList = categoryService.listByParentId(parentId);
        // 查询出我的分类
        List<CategorylList2VO> categorylList2VOList = categoryList.stream()
                .map(category -> {
                    CategorylList2VO categorylList2VO = new CategorylList2VO();
                    categorylList2VO.setCategoryId(category.getId());
                    categorylList2VO.setCategoryName(category.getName());
                    return categorylList2VO;
                }).collect(Collectors.toList());
        return Result.success(categorylList2VOList);
    }


}
